package com.me.brainlessbirds.actors.zombies;

import java.util.ArrayList;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Timer.Task;
import com.me.brainlessbirds.actors.GameObject;
import com.me.brainlessbirds.actors.Unit;
import com.me.brainlessbirds.actors.obstacles.Obstacle;
import com.me.brainlessbirds.actors.obstacles.units.Barricade;
import com.me.brainlessbirds.actors.obstacles.units.Bumper;
import com.me.brainlessbirds.actors.obstacles.units.Mine;
import com.me.brainlessbirds.actors.skills.BaseAttack;
import com.me.brainlessbirds.actors.survivors.Survivor;
import com.me.brainlessbirds.actors.survivors.units.Civilian;
import com.me.brainlessbirds.actors.survivors.units.Engineer;
import com.me.brainlessbirds.actors.survivors.units.Flamethrower;
import com.me.brainlessbirds.actors.survivors.units.Soldier;
import com.me.brainlessbirds.actors.zombies.units.Brainless;
import com.me.brainlessbirds.actors.zombies.units.Smoothie;
import com.me.brainlessbirds.actors.zombies.units.Undertaker;
import com.me.brainlessbirds.actors.zombies.units.Winehouse;
import com.me.brainlessbirds.screens.GameScreen;

public abstract class Zombie extends Unit
{
	public enum ZombieType
	{
		BRAINLESS, SMOOTHIE, UNDERTAKER, WINEHOUSE;
	}

	protected ZombieType	type	= null;

	public void start(Stage stage, float x, float y)
	{
		rgn = rgnIdle;
		setPosition(x, y - getHeight() / 2);
		addMoveToRight(stage);
	}

	public void addMoveToRight(Stage stage)
	{
		super.addMoveToRight(stage, new Task()
		{
			@Override
			public void run()
			{
				escape();
			}
		});
	}

	public void escape()
	{
		GameScreen.get().zombieEscaped();
		destroy();
		this.life = 0f;
	}

	public void kill()
	{
		this.life = 0f;
	}

	public void destroy()
	{
		super.destroy(new Task()
		{
			@Override
			public void run()
			{
				GameScreen.get().createParticleEffect(Zombie.this.getX() + Zombie.this.getWidth() / 2, Zombie.this.getY() + Zombie.this.getHeight() / 2);
				if (Zombie.this instanceof Brainless)
				{
					((Brainless) Zombie.this).resetSelector();
				}
				Zombie.this.remove();
			}
		});
	}

	@Override
	public boolean collideRight(GameObject actor)
	{
		boolean isColliding = super.collideRight(actor);
		if (isColliding)
		{
			if (actor instanceof Survivor)
			{
				Survivor s = (Survivor) actor;
				this.setBlocked(true);
				s.setBlocked(true);

				if (Zombie.this instanceof Brainless)
				{
					((Brainless) Zombie.this).resetSelector();
				}

				// TODO : animation d'attaque
				if (this instanceof Brainless || this instanceof Undertaker || this instanceof Winehouse)
				{
					rgn = rgnAtk;
				}
			}
			else if (actor instanceof Obstacle)
			{
				Obstacle o = (Obstacle) actor;
				if (!o.isDown())
				{
					if (o instanceof Barricade)
					{
						this.setBlocked(true);

						if (this instanceof Brainless || this instanceof Undertaker || this instanceof Winehouse)
						{
							rgn = rgnAtk;
						}
					}
					else if (o.getX() - this.getX() < 20f)
					{
						if (o instanceof Mine && !(this instanceof Smoothie) && !(this instanceof Winehouse))
						{
							this.life = 0f;
						}
						else if (o instanceof Bumper)
						{
							this.bumped = true;
						}
						if (!(this instanceof Winehouse))
						{
							o.hit(1f);
						}
					}
				}

				if (Zombie.this instanceof Brainless)
				{
					((Brainless) Zombie.this).resetSelector();
				}
			}
			else if (actor instanceof Zombie)
			{
				//this.setBlocked(true);
			}
		}

		return isColliding;
	}

	// Zombies

	@Override
	public void OnHitByBrainless(Brainless _b)
	{
	}

	@Override
	public void OnHitBySmoothie(Smoothie _s)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void OnHitByUndertaker(Undertaker _u)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void OnHitByWinehouse(Winehouse _w)
	{
		// TODO Auto-generated method stub

	}

	//Survivors

	@Override
	public void OnHitByCivilian(Civilian _c)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_c.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}

	@Override
	public void OnHitByEngineer(Engineer _e)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_e.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}

	@Override
	public void OnHitByFlamethrower(Flamethrower _f)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_f.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}

	@Override
	public void OnHitBySoldier(Soldier _s)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_s.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}
}
